from scipy.io import arff
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 从文件读取数据
def load_data(filenames):
    x_train = pd.DataFrame()
    for fileName in filenames:
        data, meta = arff.loadarff(fileName)
        x = pd.DataFrame(data)
        x_train = pd.concat([x_train, x], ignore_index=True)
    y_train = x_train['Defective']
    y_train = mapit(y_train)
    del x_train['Defective']
    x_train = x_train.values
    return  x_train,y_train

#将布尔值转换为0和1
def mapit(vector):

    s = np.unique(vector)

    mapping = pd.Series([x[0] for x in enumerate(s)], index = s)
    vector=vector.map(mapping)
    return vector

#从特征向量中抽取两个属性用于数据可视化
def show_data(x,y,title):
    negative = np.where(y>0) # 有缺陷
    positive = np.where(y<1) # 无缺陷

    plt.scatter(x[positive,0],  x[positive, 5],  color='g',marker='.',label='positive')
    plt.scatter(x[negative,0], x[negative,  5],  color='r', marker='x',label='negative')
    plt.xlim(0, 200)
    plt.ylim(0, 80)
    plt.xlabel('LOC_TOTAL numeric', fontsize=9)
    plt.ylabel('NUM_UNIQUE_OPERANDS numeric', fontsize=9)
    plt.title(title)
    plt.legend()
    plt.show()

def load_file_data(filename):
    X, Y = load_data(['../dataset/ar1.arff', '../dataset/ar3.arff', '../dataset/ar4.arff', '../dataset/ar5.arff',
                      '../dataset/ar6.arff'])

    # 训练集
    x_train = X[0:int(X.shape[0] * 0.6), :]
    y_train = Y[0:int(Y.shape[0] * 0.6)]
    print('x_train:{}'.format(x_train.shape))
    print('y_train:{}'.format(y_train.shape))
    show_data(x_train, y_train, 'Train Data')

    # 验证集
    x_cross = X[int(X.shape[0] * 0.6) + 1: int(X.shape[0] * 0.8), :]
    y_cross = Y[int(Y.shape[0] * 0.6) + 1: int(Y.shape[0] * 0.8)]
    print('x_cross:{}'.format(x_cross.shape))
    print('y_cross:{}'.format(y_cross.shape))
    show_data(x_cross, y_cross, 'Cross Data')

    # 测试集
    x_test = X[int(X.shape[0] * 0.8 + 1):, :]
    y_test = Y[int(Y.shape[0] * 0.8) + 1:]
    print('x_test:{}'.format(x_test.shape))
    print('y_test:{}'.format(y_test.shape))
    show_data(x_test, y_test, 'Test Data')


X,Y = load_data(['../dataset/ar1.arff','../dataset/ar3.arff','../dataset/ar4.arff','../dataset/ar5.arff','../dataset/ar6.arff'])

#训练集
x_train = X[0:int(X.shape[0]*0.6),:]
y_train = Y[0:int(Y.shape[0]*0.6)]
print('x_train:{}'.format(x_train.shape))
print('y_train:{}'.format(y_train.shape))
show_data(x_train,y_train,'Train Data')


#验证集
x_cross = X[int(X.shape[0]*0.6)+1: int(X.shape[0]*0.8),:]
y_cross = Y[int(Y.shape[0]*0.6)+1: int(Y.shape[0]*0.8) ]
print('x_cross:{}'.format(x_cross.shape))
print('y_cross:{}'.format(y_cross.shape))
show_data(x_cross,y_cross,'Cross Data')

#测试集
x_test = X[int(X.shape[0]*0.8+1):,:]
y_test = Y[int(Y.shape[0]*0.8)+1: ]
print('x_test:{}'.format(x_test.shape))
print('y_test:{}'.format(y_test.shape))
show_data(x_test,y_test,'Test Data')




